"""Trading system.

At the moment I am not sure about the workflow here, but I will start to make things
appear nonetheless.

Inspired so far in UltraFinance [1]


References:
[1] http://code.google.com/p/ultra-finance/

"""


"""

The so-called "feeds" are still not clear to me. What does it do?

"""


class Account:
    """What does account represent?
    
    I can't think about anything except for name and cash
    
    The account has a list of holdings
    """
    
    def __init__(self):
        # Account name
        self.name = "Unnamed account"
        # Amount of cash
        self.cash = 0
        # Holdings: dictionary: {stock name: number of papers, ...}



class TradingManager():
    """TODO: Lacking a better name.
    
    This class has some abstract methods: buy, sell, not sure about sellShort and cover
    
    This class will have descendants. Examples: back testing will accept all requests
    from the trading system. A Realtime trqading manager will publish the "hints"
    on screen and allow for the user (me) to check whether I have accepted the hints,
    number of shares and price, this all done in ASYNCHRONOUS manner.
    
    Perhaps the Buy and Sell functions can return the number of boughtsold shares, and
    in addition the status of the trading system can be changed asynchronously as well.
    
    """


    # These Callback functions are called by a TradingSystem object

    def Buy(self):
        pass
        
    def Sell(self):
        pass
        
    def SellShort(self):
        pass
    def Cover(self):
        pass
        
        
    def InformTradingSystemThatHasBoughtSomething(self, amount):
        pass
        
class TradingSystem(QObject):
    """Object responsible for analysing streams and producing the following:
    
    - buy, sell, sell short and cover signals.
    - start and stop thresholds??
    
    I think that it needs a status: out/bought/sold
    If status is "out", can only generate buy or sellShort signals.
    If stauts is "bought", can only generate a sell signals
    If status is "sold", can only generate a cover signal.
    
    I still don't know how to update this status. Could be automatic, self-contained,
    but it will work only for backtesting. I will also need my TradingSystem to give
    me realtime signals and give it feedback of whether I will accept these signals
    or not.
    
    """
    def __init__(self):
        self.account = None
    def Buy(self):
        pass
    def Sell(self):
        pass
    def SellShort(self):
        pass
    def Cover(self):
        pass
        
        
        
    def InjectTimeData(self, td):
        """Gives data to analyse. I honestly don't know what will be synchnous or not
        here."""
        pass
        
    def AnalyseTD(self):
        while True:
            # Call CheckBuy/CheckSell/CheckSellShort/CheckCover, depending
            #on current status
            # These functions must return a "Suggestion" object, with the obvious
            # yes/no field, but maybe also confidence levels and who knows.
            
            # Keeps looping until end of TD
            
            
            
            
            '''PRobably the first confusing thing to solve is how to access a TimeData
            object with running reference time. Have a look at the indicators that I
            have already coded.
            
            The good news is that this is probably the biggest design question of my
            project.
            
            '''
